/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.es.api;

import com.pingan.haofang.searchcloud.api.CollectionManagerService;
import com.pingan.haofang.searchcloud.api.CollectionSummaryDto;
import com.pingan.haofang.searchcloud.api.DiskConditonDTO;
import com.pingan.haofang.searchcloud.api.constants.EngineType;
import com.pingan.haofang.searchcloud.api.matedata.CollectionMetaData;
import com.pingan.haofang.searchcloud.es.api.excption.IndicesException;
import com.pingan.haofang.searchcloud.es.indices.IndicesManagerService;
import com.pingan.haofang.searchcloud.es.utils.EsUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * es collection 管理
 *
 * @author zhangbi617
 * @date 2017-07-17
 */
@Service
public class EsCollectionManagerService implements CollectionManagerService {

    @Autowired
    private IndicesManagerService indicesManagerService;

    @Override
    public EngineType getEngineType() {
        return EngineType.ES;
    }

    @Override
    public List<CollectionMetaData> list(String clusterHost) throws Exception {
        throw new IndicesException("not implements");
    }

    @Override
    public CollectionMetaData get(String clusterHost, String collectionName) throws Exception {
        throw new IndicesException("not implements");
    }

    @Override
    public boolean create(String clusterHost, CollectionMetaData collectionMetaData) throws Exception {
        CollectionMetaData clone = (CollectionMetaData) BeanUtils.cloneBean(collectionMetaData);
        clone.setName(EsUtils.nameWithPrefix(clone.getName()));
        return indicesManagerService.create(clusterHost, clone);

    }

    @Override
    public boolean delete(String clusterHost, String collectionName) throws Exception {
        return indicesManagerService.delete(clusterHost, EsUtils.nameWithPrefix(collectionName));
    }

    @Override
    public List<String> getAllCollectionNames(String clusterHost) throws Exception {
        return indicesManagerService.getIndicesNames(clusterHost);
    }

    @Override
    public Map<String, CollectionSummaryDto> getAllCollectionSummary(String clusterHost, List<CollectionMetaData> list) {
        return new HashMap<>();
    }

    @Override
    public List<String> getLiveHostUrl(String clusterHost) {
        return indicesManagerService.getLiveHostUrl(clusterHost);
    }

    @Override
    public Map<String, DiskConditonDTO> getNodesSummary(String clusterHost) {
        return indicesManagerService.getNodesSummary(clusterHost);
    }
}
